<?php

/********* Page Templates v.1.8 ************/

define( 'REALESTATE_PAGE_TEMPLATE_PATH', get_template_directory_uri() . '/epanel/page_templates' );

add_action('wp_print_styles','realestate_page_template_css');
function realestate_page_template_css(){
	if ( !is_admin() && !(strstr( $_SERVER['PHP_SELF'], 'wp-login.php')) ) {
		wp_enqueue_style( 'fancybox', REALESTATE_PAGE_TEMPLATE_PATH . '/js/fancybox/jquery.fancybox-1.3.4.css', array(), '1.3.4', 'screen' );
		wp_enqueue_style( 'realestate_page_templates', REALESTATE_PAGE_TEMPLATE_PATH . '/page_templates.css', array(), '1.8', 'screen' );
	}
}

add_action('wp_print_scripts','realestate_page_template_footer_js');
function realestate_page_template_footer_js(){
	if ( !is_admin() ) {
		wp_enqueue_script('easing', REALESTATE_PAGE_TEMPLATE_PATH . '/js/fancybox/jquery.easing-1.3.pack.js', array('jquery'), '1.3.4', true);
		wp_enqueue_script('fancybox', REALESTATE_PAGE_TEMPLATE_PATH . '/js/fancybox/jquery.fancybox-1.3.4.pack.js', array('jquery'), '1.3.4', true);
		wp_enqueue_script('realestate-page-template-frontend', REALESTATE_PAGE_TEMPLATE_PATH . '/js/realestate-page-template-frontend.js', array('jquery','fancybox'), '1.1', true);
	}
}

add_action( 'admin_enqueue_scripts', 'realestate_page_template_upload_categories_scripts' );
function realestate_page_template_upload_categories_scripts( $hook_suffix ) {
	if ( in_array($hook_suffix, array('post.php','post-new.php')) ) {
		wp_register_script('realestate-page-template', get_template_directory_uri().'/epanel/page_templates/js/realestate-page-template.js', array('jquery'));
		wp_enqueue_script('realestate-page-template');
	}
}

add_action("admin_init", "realestate_page_template_metabox");
function realestate_page_template_metabox(){
	global $themename;
	add_meta_box("realestate_page_template_meta", esc_html__( 'Real Estate Page Template Settings', $themename ), "realestate_page_template_meta", "page", "side");
}

if ( ! function_exists( 'realestate_page_template_meta' ) ){
	function realestate_page_template_meta($callback_args) {
		global $post, $themename;
		$temp_array = array();

		$temp_array = maybe_unserialize(get_post_meta($post->ID,'realestate_page_template_settings',true));

		$realestate_fullwidthpage = isset( $temp_array['realestate_fullwidthpage'] ) ? (bool) $temp_array['realestate_fullwidthpage'] : false;
		$realestate_regenerate_numbers = isset( $temp_array['realestate_regenerate_numbers'] ) ? (bool) $temp_array['realestate_regenerate_numbers'] : false;
		$realestate_page_template_blogstyle = isset( $temp_array['realestate_page_template_blogstyle'] ) ? (bool) $temp_array['realestate_page_template_blogstyle'] : false;
		$realestate_page_template_showthumb = isset( $temp_array['realestate_page_template_showthumb'] ) ? (bool) $temp_array['realestate_page_template_showthumb'] : false;
		$realestate_page_template_blogcats = isset( $temp_array['realestate_page_template_blogcats'] ) ? (array) $temp_array['realestate_page_template_blogcats'] : array();
		$realestate_page_template_gallerycats = isset( $temp_array['realestate_page_template_gallerycats'] ) ? (array) $temp_array['realestate_page_template_gallerycats'] : array();
		$realestate_page_template_blog_perpage = isset( $temp_array['realestate_page_template_blog_perpage'] ) ? (int) $temp_array['realestate_page_template_blog_perpage'] : 10;
		$realestate_page_template_gallery_perpage = isset( $temp_array['realestate_page_template_gallery_perpage'] ) ? (int) $temp_array['realestate_page_template_gallery_perpage'] : 10;
		$realestate_email_to = isset( $temp_array['realestate_email_to'] ) ? esc_attr( $temp_array['realestate_email_to'] ) : '';
		$realestate_page_template_showtitle = isset( $temp_array['realestate_page_template_showtitle'] ) ? (bool) $temp_array['realestate_page_template_showtitle'] : 1;
		$realestate_page_template_showdesc = isset( $temp_array['realestate_page_template_showdesc'] ) ? (bool) $temp_array['realestate_page_template_showdesc'] : 1;
		$realestate_page_template_detect_portrait = isset( $temp_array['realestate_page_template_detect_portrait'] ) ? (bool) $temp_array['realestate_page_template_detect_portrait'] : 1;
		$realestate_page_template_imagesize = isset( $temp_array['realestate_page_template_imagesize'] ) ? (int) $temp_array['realestate_page_template_imagesize'] : 2;	?>

		<?php wp_nonce_field( 'realestate_page_template_nonce', '_wpnonce_page_template_save' ); ?>

		<div style="margin: 13px 0 11px 4px;" class="realestate_page_template_info">
			<p><?php esc_html_e( 'Additional settings appear here, when one of real estate page templates is selected ( Page Attributes -> Template )', $themename ); ?></p>
		</div>

		<div style="margin: 13px 0 11px 4px; display: none;" class="realestate_page_template_sitemap realestate_page_template_blog realestate_page_template_gallery realestate_page_template_search realestate_page_template_login realestate_page_template_contact realestate_page_template_portfolio">
			<label class="selectit" for="realestate_fullwidthpage">
				<input type="checkbox" name="realestate_fullwidthpage" id="realestate_fullwidthpage" value=""<?php checked( $realestate_fullwidthpage ); ?> /> <?php esc_html_e( 'Full Width Page', $themename ); ?></label><br/>
		</div>

		<div style="margin: 13px 0 11px 4px; display: none;" class="realestate_page_template_contact">
			<label class="selectit" for="realestate_regenerate_numbers">
				<input type="checkbox" name="realestate_regenerate_numbers" id="realestate_regenerate_numbers" value=""<?php checked( $realestate_regenerate_numbers ); ?> /> <?php esc_html_e( 'Regenerate captcha numbers', $themename ); ?></label><br/>
		</div>

		<div style="margin: 13px 0 11px 4px; display: none;" class="realestate_page_template_contact">
			<label for="realestate_email_to" style="color: #000; font-weight: bold;"> <?php esc_html_e( 'Email To:', $themename ); ?> </label>
			<input type="text" value="<?php echo esc_attr( $realestate_email_to ); ?>" id="realestate_email_to" name="realestate_email_to" size="20" />
		</div>

		<div style="margin: 13px 0 11px 4px; display: none;" class="realestate_page_template_blog">
			<label class="selectit" for="realestate_page_template_blogstyle">
				<input type="checkbox" name="realestate_page_template_blogstyle" id="realestate_page_template_blogstyle" value=""<?php checked( $realestate_page_template_blogstyle ); ?> /> <?php esc_html_e( 'Blog Style mode', $themename ); ?></label><br/>
		</div>

		<div style="margin: 13px 0 11px 4px; display: none;" class="realestate_page_template_blog">
			<label class="selectit" for="realestate_page_template_showthumb">
				<input type="checkbox" name="realestate_page_template_showthumb" id="realestate_page_template_showthumb" value=""<?php checked( $realestate_page_template_showthumb ); ?> /> <?php esc_html_e( 'Hide Auto Thumbnail', $themename ); ?></label><br/>
		</div>

		<div style="margin: 13px 0 11px 4px; display: none;" class="realestate_page_template_portfolio">
			<label class="selectit" for="realestate_page_template_showtitle">
				<input type="checkbox" name="realestate_page_template_showtitle" id="realestate_page_template_showtitle" value=""<?php checked( $realestate_page_template_showtitle ); ?> /> <?php esc_html_e( 'Show Titles', $themename ); ?></label><br/>
		</div>

		<div style="margin: 13px 0 11px 4px; display: none;" class="realestate_page_template_portfolio">
			<label class="selectit" for="realestate_page_template_showdesc">
				<input type="checkbox" name="realestate_page_template_showdesc" id="realestate_page_template_showdesc" value=""<?php checked( $realestate_page_template_showdesc ); ?> /> <?php esc_html_e( 'Show Descriptions', $themename ); ?></label><br/>
		</div>

		<div style="margin: 13px 0 11px 4px; display: none;" class="realestate_page_template_portfolio">
			<label class="selectit" for="realestate_page_template_detect_portrait">
				<input type="checkbox" name="realestate_page_template_detect_portrait" id="realestate_page_template_detect_portrait" value=""<?php checked( $realestate_page_template_detect_portrait ); ?> /> <?php esc_html_e( 'Detect Portrait Images', $themename ); ?></label><br/>
		</div>

		<div style="margin: 13px 0 11px 4px; display: none;" class="realestate_page_template_portfolio">
			<p style="font-weight: bold; padding-bottom: 7px;"><?php esc_html_e( 'Thumbnail Size:', $themename ); ?></p>
			<label title="Small"><input type="radio" name="realestate_page_template_imagesize" value="1"<?php checked( $realestate_page_template_imagesize, 1 ); ?>> <span><?php esc_html_e( 'Small', $themename ); ?></span></label><br><br>
			<label title="Medium"><input type="radio" name="realestate_page_template_imagesize" value="2"<?php checked( $realestate_page_template_imagesize, 2 ); ?>> <span><?php esc_html_e( 'Medium', $themename ); ?></span></label><br><br>
			<label title="Large"><input type="radio" name="realestate_page_template_imagesize" value="3"<?php checked( $realestate_page_template_imagesize, 3 ); ?>> <span><?php esc_html_e( 'Large', $themename ); ?></span></label>
		</div>

		<div style="margin: 13px 0 11px 4px; display: none;" class="realestate_page_template_blog">
			<label for="realestate_page_template_blog_perpage" style="color: #000; font-weight: bold;"> <?php esc_html_e( 'Number of posts per page:', $themename ); ?> </label>
			<input type="text" class="small-text" value="<?php echo esc_attr( $realestate_page_template_blog_perpage ); ?>" id="realestate_page_template_blog_perpage" name="realestate_page_template_blog_perpage" size="2" />
		</div>

		<div style="margin: 13px 0 11px 4px; display: none;" class="realestate_page_template_gallery realestate_page_template_portfolio">
			<label for="realestate_page_template_gallery_perpage" style="color: #000; font-weight: bold;"> <?php esc_html_e( 'Number of posts per page:', $themename ); ?> </label>
			<input type="text" class="small-text" value="<?php echo $realestate_page_template_gallery_perpage; ?>" id="realestate_page_template_gallery_perpage" name="realestate_page_template_gallery_perpage" size="2" />
		</div>

		<div style="margin: 13px 0 11px 4px; display: none;" class="realestate_page_template_blog">
			<h4><?php esc_html_e( 'Select blog categories:', $themename ); ?></h4>

			<?php $cats_array = get_categories('hide_empty=0');
			$site_cats = array();
			foreach ($cats_array as $categs) {
				$checked = '';

				if (!empty($realestate_page_template_blogcats)) {
					if (in_array($categs->cat_ID, $realestate_page_template_blogcats)) $checked = "checked=\"checked\"";
				} ?>

				<label style="padding-bottom: 5px; display: block;" for="<?php echo esc_attr( 'realestate_page_template_blogcats-' . $categs->cat_ID ); ?>">
					<input type="checkbox" name="realestate_page_template_blogcats[]" id="<?php echo esc_attr( 'realestate_page_template_blogcats-' . $categs->cat_ID ); ?>" value="<?php echo esc_attr($categs->cat_ID); ?>" <?php echo $checked; ?> />
					<?php echo esc_html( $categs->cat_name ); ?>
				</label>
			<?php } ?>
		</div>

		<div style="margin: 13px 0 11px 4px; display: none;" class="realestate_page_template_gallery realestate_page_template_portfolio">
			<h4><?php esc_html_e( 'Select gallery categories:', $themename ); ?></h4>

			<?php $cats_array = get_categories('hide_empty=0');
			$site_cats = array();
			foreach ($cats_array as $categs) {
				$checked = '';

				if (!empty($realestate_page_template_gallerycats)) {
					if (in_array($categs->cat_ID, $realestate_page_template_gallerycats)) $checked = "checked=\"checked\"";
				} ?>

				<label style="padding-bottom: 5px; display: block;" for="<?php echo 'realestate_page_template_gallerycats-',$categs->cat_ID; ?>">
					<input type="checkbox" name="realestate_page_template_gallerycats[]" id="<?php echo esc_attr( 'realestate_page_template_gallerycats-' . $categs->cat_ID ); ?>" value="<?php echo esc_attr( $categs->cat_ID ); ?>" <?php echo $checked; ?> />
					<?php echo esc_html( $categs->cat_name ); ?>
				</label>
			<?php } ?>
		</div>

		<?php
	}
}

add_action( 'save_post', 'realestate_page_template_save_details', 10, 2 );
function realestate_page_template_save_details( $post_id, $post ){
	global $pagenow;

	if ( 'post.php' != $pagenow ) return $post_id;

	if ( 'page' != $post->post_type )
		return $post_id;

	if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
		return $post_id;

	$post_type = get_post_type_object( $post->post_type );
	if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
		return $post_id;

	if ( ! isset( $_POST['_wpnonce_page_template_save'] ) || ! wp_verify_nonce( $_POST['_wpnonce_page_template_save'], 'realestate_page_template_nonce' ) )
        return $post_id;

	if ( !isset( $_POST["page_template"] ) )
		return $post_id;

	if ( !in_array( $_POST["page_template"], array('page-blog.php', 'page-sitemap.php', 'page-gallery.php', 'page-search.php', 'page-login.php', 'page-contact.php', 'page-template-portfolio.php') ) )
		return $post_id;

	$temp_array = array();

	$temp_array['realestate_fullwidthpage'] = isset( $_POST["realestate_fullwidthpage"] ) ? 1 : 0;

	if ( 'page-blog.php' == $_POST["page_template"] ) {
		$temp_array['realestate_page_template_blogstyle'] = isset( $_POST["realestate_page_template_blogstyle"] ) ? 1 : 0;
		$temp_array['realestate_page_template_showthumb'] = isset( $_POST["realestate_page_template_showthumb"] ) ? 1 : 0;
		if (isset($_POST["realestate_page_template_blogcats"])) $temp_array['realestate_page_template_blogcats'] = (array) $_POST["realestate_page_template_blogcats"];
		if (isset($_POST["realestate_page_template_blog_perpage"])) $temp_array['realestate_page_template_blog_perpage'] = (int) $_POST["realestate_page_template_blog_perpage"];
	}

	if ( 'page-gallery.php' == $_POST["page_template"] ) {
		if (isset($_POST["realestate_page_template_gallerycats"])) $temp_array['realestate_page_template_gallerycats'] = (array) $_POST["realestate_page_template_gallerycats"];
		if (isset($_POST["realestate_page_template_gallery_perpage"])) $temp_array['realestate_page_template_gallery_perpage'] = (int) $_POST["realestate_page_template_gallery_perpage"];
	}

	if ( 'page-template-portfolio.php' == $_POST["page_template"] ) {
		if (isset($_POST["realestate_page_template_gallerycats"])) $temp_array['realestate_page_template_gallerycats'] = (array) $_POST["realestate_page_template_gallerycats"];
		if (isset($_POST["realestate_page_template_gallery_perpage"])) $temp_array['realestate_page_template_gallery_perpage'] = (int) $_POST["realestate_page_template_gallery_perpage"];
		$temp_array['realestate_page_template_showtitle'] = isset( $_POST["realestate_page_template_showtitle"] ) ? 1 : 0;
		$temp_array['realestate_page_template_showdesc'] = isset( $_POST["realestate_page_template_showdesc"] ) ? 1 : 0;
		$temp_array['realestate_page_template_detect_portrait'] = isset( $_POST["realestate_page_template_detect_portrait"] ) ? 1 : 0;
		$temp_array['realestate_page_template_imagesize'] = isset( $_POST["realestate_page_template_imagesize"] ) ? (int) $_POST["realestate_page_template_imagesize"] : 2;
	}

	if ( 'page-contact.php' == $_POST["page_template"] ) {
		$temp_array['realestate_regenerate_numbers'] = isset( $_POST["realestate_regenerate_numbers"] ) ? 1 : 0;
		if (isset($_POST["realestate_email_to"])) $temp_array['realestate_email_to'] = sanitize_email( $_POST["realestate_email_to"] );
	}

	update_post_meta( $post_id, "realestate_page_template_settings", $temp_array );
} ?>